﻿using System.Collections.Generic;
using System.IO;
using Caliburn.Micro;
using Microsoft.Phone.Tasks;
using Ninject;
using Spotted.Core;
using Spotted.Data.Models;
using Spotted.Data.Repositories;

namespace Spotted.ViewModels
{
    public class AddLocationViewModel : ViewModelBase
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly IAppService _appService;

        public double CenterLatitude { get; set; }
        public double CenterLongitude { get; set; }

        // properties
        public string SpotName { get; set; }
        public string SpotDescription { get; set; }
        public SpotIntention SpotIntention { get; set; }
        public byte[] SpotPicture { get; private set; }

        // UI resource properties
        public IList<SpotIntention> SpotIntentions
        {
            get { return _appService.GetSpotIntentions(); }
        }

        // Dependencies
        [Inject]
        public ISpotRepository SpotRepository { get; set; }

        [Inject]
        public AddLocationViewModel(IEventAggregator eventAggregator, INavigationService navigationService, IAppService appService) : base(navigationService)
        {
            // assign private members
            _eventAggregator = eventAggregator;
            _appService = appService;

            // assign special defaults
            SpotIntention = SpotIntentions[0];
        }

        public void AddPicture()
        {
            _eventAggregator.RequestTask<CameraCaptureTask>(task =>
                                                                {
                                                                    task.Completed += TakePictureCompleted;
                                                                    task.Show();
                                                                });
        }

        public void TakePictureCompleted(object sender, PhotoResult photoResult)
        {
            // prepare the package for transport (convert to byte[])
            if (photoResult.ChosenPhoto != null)
            {
                using (var reader = new BinaryReader(photoResult.ChosenPhoto))
                {
                    long length = photoResult.ChosenPhoto.Length;
                    byte[] imageData = reader.ReadBytes((int) length);
                    SpotPicture = imageData;
                    NotifyOfPropertyChange("SpotPicture");
                }
            }
        }

        public void SavePlace()
        {
            _appService.ShowProgressIndicator("saving spot");
            var vm = new AddNewSpotViewModel
                         {
                             Name = SpotName,
                             Description = SpotDescription,
                             IntentionId = SpotIntention.Id,
                             Latitude = CenterLatitude,
                             Longitude = CenterLongitude,
                             ImageData = SpotPicture
                         };

            SpotRepository.AddNewSpotCompleted += SpotRepository_AddNewSpotCompleted;
            SpotRepository.AddNewSpot(vm);
        }

        void SpotRepository_AddNewSpotCompleted(object sender, System.EventArgs e)
        {
            throw new System.NotImplementedException();
        }
    }
}
